package com.cjg.scrolldemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.cjg.scrolldemo.databinding.ActivityPersonBinding;
import com.cjg.scrolldemo.databinding.FragmentCPersonBinding;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class PersonActivity extends AppCompatActivity {
    private static final String TAG = "~PersonActivity";

    private FragmentCPersonBinding binding;
    public static ObservableField<String> n = new ObservableField<>("333");
    private final String[] TAB_TITLES={"帖子", "收藏"};
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this,R.layout.fragment_c_person);//binding = ActivityPersonBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());

        initHeaderAnim();
        intTabViewPage();
    }

    private void intTabViewPage() {
//        fragments.add(new CPersonFragment());
//        fragments.add(new CPersonFragment());
        CPersonPagerViewAdapter pagerAdapter = new CPersonPagerViewAdapter(this,getSupportFragmentManager());
        binding.viewPager.setAdapter(pagerAdapter);
        binding.tabs.setupWithViewPager(binding.viewPager);
        //binding.viewPager.setUserInputEnabled(false);//禁止左右滑动 binding.viewPager.setOffscreenPageLimit(1); //预加载页面数
//        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {//页面切换监听
//            @Override
//            public void onPageSelected(int position) {
//                super.onPageSelected(position);
//                Log.d(TAG, "ViewPager onPageSelected " + position);
//            }
//        });

//        //Tab绑定ViewPage，这里必须要在添加适配器完了之后才能新建该方法
//        new TabLayoutMediator(binding.tabs,binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
//            @Override
//            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//                tab.setText(TAB_TITLES[position]);//就是将标题内容分别相对应的赋值
//            }
//        }).attach();
    }

    private void initHeaderAnim() {
        binding.appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                float alpha = -verticalOffset * 1.0f / appBarLayout.getTotalScrollRange();
                binding.toolbarBg.setAlpha(alpha > 0.7 ? (float) Math.pow(alpha, 3) : 0);
                binding.title.setAlpha(alpha > 0.7 ? (float) Math.pow(alpha, 3) : 0);
                binding.titleCenterLayout.setAlpha(alpha > 0.7 ? (1-alpha): 1);
            }
        });
    }



    @Override
    public void onBackPressed() {
        n.set("嘻嘻嘻");
    }

    public void onClickOne(View view) {
        Log.e(TAG, "onClickOne: ");
        Toast.makeText(this, "xxxx", Toast.LENGTH_SHORT).show();
    }

}




